package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Diagnosis;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.DiagnosisService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "farm/sysuser/diagnosis", tags = "诊断信息管理模块")  //描述信息用的，标注在类上
@Slf4j
@RestController
@RequestMapping("farm/sysuser/diagnosis")
public class DiagnosisController {
    @Autowired
    DiagnosisService diagnosisService;

    @ApiOperation(value = "诊断信息添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult diagnosisAdd(Diagnosis diagnosis) {
        try {
            diagnosisService.add(diagnosis);
            log.info("诊断信息编号为:{}数据添加成功", diagnosis.getInquiryid());
            return CommonResult.success(diagnosis);
        } catch (DataNotExistException e) {
            log.error("诊断信息编号为:{}的诊断信息添加失败", diagnosis.getInquiryid());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 诊断删除
     */
    @ApiOperation(value = "诊断删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult diagnosisDelete(@RequestParam("diagnosisid") Integer diagnosisid) {
        try {
            diagnosisService.delete(diagnosisid);
            log.info("诊断信息编号为：{}的诊断信息删除成功", diagnosisid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("诊断信息删除失败");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 诊断信息查询
     */
    @ApiOperation(value = "诊断信息分页查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Diagnosis>> diagnosisList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                           @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Diagnosis> diagnosis = diagnosisService.selectAll(page, pagesize);
            log.info("查询第{}页的诊断信息，共有{}个", page, pagesize);
            return CommonResult.success(diagnosis);
        } catch (DataNotExistException e) {
            log.error("不存在诊断分析数据，查询失败");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
